/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.atomojo.tools.media;

import java.util.Date;
import java.util.logging.Logger;
import org.restlet.Client;
import org.restlet.Context;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.data.ChallengeRequest;
import org.restlet.data.ChallengeResponse;
import org.restlet.data.Cookie;
import org.restlet.data.CookieSetting;
import org.restlet.data.Method;
import org.restlet.data.Reference;
import org.restlet.data.Status;
import org.restlet.engine.header.Header;
import org.restlet.engine.util.DateUtils;
import org.restlet.util.Series;

/**
 *
 * @author alex
 */
public class MediaBrowser {

   /*
   class Cookies extends Series<Cookie> {
      public Cookie createEntry(String name, String value) {
         return new Cookie(name,value);
      }
      public Series<Cookie> createSeries(List<Cookie> delegate) {
         Cookies series = new Cookies();
         series.addAll(delegate);
         return series;
      }
   }
    */
   Context context;
   Client client;
   Series<Cookie> cookies;
   String username;
   String password;
   public MediaBrowser(Context context) {
      this.context = context;
      this.client = context.getClientDispatcher();
      //this.cookies = new Cookies();
      this.cookies = new Series<Cookie>(Cookie.class);
      this.username = null;
      this.password = null;
   }
   
   public Context getContext() {
      return context;
   }
   
   public Logger getLogger() {
      return context.getLogger();
   }
   
   public void setIdentity(String username, String password) {
      this.username = username;
      this.password = password;
   }
   
   public Request newRequest(Method method,Reference href) {
      Request request = new Request(method,href);
      // TOOD: handle path for cookies
      request.setCookies(cookies);
      return request;
   }
   
   public Response attemptCall(Request request) {
      Response response = client.handle(request);
      if (username!=null && response.getStatus().equals(Status.CLIENT_ERROR_UNAUTHORIZED)) {
         getLogger().info("Retrying with challenge response...");
         request.setChallengeResponse(null);
         for (ChallengeRequest challenge : response.getChallengeRequests()) {
            request.setChallengeResponse(new ChallengeResponse(challenge,response,username,password.toCharArray()));
         }
         if (request.getChallengeResponse()!=null) {
            response = client.handle(request);
         }
      }
      filterResponse(response);
      return response;
   }
   
   public void filterResponse(Response response) {
      for (CookieSetting setting : response.getCookieSettings()) {
         getLogger().info("Setting cookie: "+setting.getName()+"="+setting.getValue());
         cookies.removeAll(setting.getName());
         cookies.add(setting.getName(),setting.getValue());
      }
   }
   
   public void addSlug(Request request,String slug) {
      Series<Header> headers = (Series<Header>)request.getAttributes().get("org.restlet.http.headers");
      if (headers==null) {
         headers = new Series<Header>(Header.class);
         request.getAttributes().put("org.restlet.http.headers", headers);
      }
      headers.add("Slug", Reference.encode(slug));
   }
   public void addXLastModified(Request request,Date date) {
      Series<Header> headers = (Series<Header>)request.getAttributes().get("org.restlet.http.headers");
      if (headers==null) {
         headers = new Series<Header>(Header.class);
         request.getAttributes().put("org.restlet.http.headers", headers);
      }
      headers.add("X-Last-Modified", DateUtils.format(date));
   }
}
